package com.hongdee.scm.countforms.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.DateUtils;
import com.hongdee.common.util.StringUtil;
import com.hongdee.scm.countforms.entity.TScmMonthCar;
import com.hongdee.scm.countforms.service.ITScmCountService;
import com.hongdee.scm.repertory.entity.TScmReperory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * 名  称:TscmCountController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.countforms.controller
 * 作  者: 张善姬
 * 日  期:2017-07-25 09:22
 */
@RestController
@RequestMapping("/scm/count")
public class TscmCountController {

    @Autowired
    private ITScmCountService itScmCountService;

    /**
     * 日报
     * @param uid
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/countByDay")
    @ResponseBody
    public Map countByDay(@RequestParam(name = "uid", defaultValue = "") String uid,
                          @RequestParam(name = "token", defaultValue = "") String token,
                          @RequestParam(name = "start", defaultValue = "0") String start,
                          @RequestParam(name = "limit", defaultValue = "50") String limit,
                          @RequestBody Map data )
            throws Exception{
        Map map = this.itScmCountService.countByDay(uid,data,start,limit);
        map.put("success",true);
        map.put("msg","查询成功");
        return map;
    }

    /**
     * 月报
     * @param uid
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/countByMonth")
    @ResponseBody
    public Map countByMonth(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "start", defaultValue = "0") String start,
                            @RequestParam(name = "limit", defaultValue = "50") String limit,
                            @RequestBody Map data )
            throws Exception{
        Map map = this.itScmCountService.countByMonth(uid,data,start,limit);
        map.put("success",true);
        map.put("msg","查询成功");
        return map;
    }

    /**
     * 润华汽车库存统计表
     * @param uid
     * @param start
     * @param limit
     * @return
     * @throws Exception
     */
    @RequestMapping("/stockCountForms")
    @ResponseBody
    public Map stockCountForms(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "start", defaultValue = "0") String start,
                               @RequestParam(name = "limit", defaultValue = "70") String limit,
                               @RequestParam(name = "month", defaultValue = "") String month)
            throws Exception{
        Map map = this.itScmCountService.stockCountForms(uid,start,limit,month);
        map.put("success",true);
        map.put("msg","查询成功");
        return map;
    }

    /**
     * 日报导出
     * @param uid
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/excel/exportDay")
    public void exportExcelDay(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "dateStart", defaultValue = "") String dateStart,
                               @RequestParam(name = "dateEnd", defaultValue = "") String dateEnd,
                            HttpServletRequest request,
                            HttpServletResponse response
    )
            throws Exception {
        List lists = itScmCountService.findDayList(uid,dateStart,dateEnd);

        HSSFWorkbook wb = itScmCountService.exportDay(lists, uid);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "日报表";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),"iso8859-1") + DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    }

    /**
     * 月报导出
     * @param uid
     * @param token
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/excel/exportMonth")
    public void exportExcelMonth(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "dateStart", defaultValue = "") String dateStart,
                               @RequestParam(name = "dateEnd", defaultValue = "") String dateEnd,
                               HttpServletRequest request,
                               HttpServletResponse response
    )
            throws Exception {
        List lists = itScmCountService.findMonthList(uid,dateStart,dateEnd);

        HSSFWorkbook wb = itScmCountService.exportMonth(lists, uid);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "月报表";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),"iso8859-1") + DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    }

    /**
     * 月报明细导出
     * @param uid
     * @param token
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/excel/exportMonthDetail")
    public void exportExcelMonthDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "dateStart", defaultValue = "") String dateStart,
                                 @RequestParam(name = "dateEnd", defaultValue = "") String dateEnd,
                                 HttpServletRequest request,
                                 HttpServletResponse response
    ) throws Exception {
        List lists = itScmCountService.findMonthDetailList(uid,dateStart,dateEnd);
        HSSFWorkbook wb = itScmCountService.exportMonthDetail(lists, uid);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "月报明细表";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),"iso8859-1") + DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    }

    /**
     * 月报穿透
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/monthThrough")
    @ResponseBody
    public Map monthThrough(@RequestParam(name = "uid", defaultValue = "") String uid,
                          @RequestParam(name = "token", defaultValue = "") String token,
                          @RequestParam(name = "start", defaultValue = "0") String start,
                          @RequestParam(name = "limit", defaultValue = "10") String limit,
                          @RequestBody Map data )
            throws Exception{
        Map map = this.itScmCountService.monthThrough(uid,data,start,limit);
        map.put("success",true);
        map.put("msg","查询成功！");
        return map;
    }

    /**
     * 润华汽车库存报表导出
     * @param uid
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/excel/exportStockForms")
    public void exportStockForms(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "month", defaultValue = "70") String month,
                               HttpServletRequest request,
                               HttpServletResponse response
    )
            throws Exception {
        List lists = itScmCountService.findStockFormsList(month);

        HSSFWorkbook wb = itScmCountService.exportStockForm(lists);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "润华汽车库存统计表";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"),"iso8859-1") + DateUtils.getTimeStr("yyyyMMddHHmmss").toString() + ".xls ");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    }

    /**
     * 月报穿透明细导出
     * @param uid
     * @param token
     * @param data
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/exportMonthCar")
    @ResponseBody
    public ResultEntity exportMonthCar(@RequestParam(name = "uid", defaultValue = "") String uid,
                                       @RequestParam(name = "token", defaultValue = "") String token,
                                       @RequestBody Map data,
                                       HttpServletRequest request) throws Exception {

        List<TScmMonthCar> dataList = this.itScmCountService.findMonthCarList(uid,data);
        XSSFWorkbook wb = this.itScmCountService.createExcel(dataList, uid);
        String path = request.getSession().getServletContext().getRealPath(".");
        String fileId = StringUtil.UUID();
        path =   path + File.separator + "upload";

        File filepack = new File(path);
        if(!filepack.exists()){
            filepack.mkdir();
        }
        String filePath =   path + File.separator + fileId + ".xlsx";
        System.out.println("文件路径：" + filePath);
        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;
    }
}
